/*
 * #%L
 * java model
 * %%
 * Copyright (C) 2009 - 2011 prefabware.com Stefan Isele
 * %%
 * All Rights Reserved.
 * This program is dual-licensed under the GNU General Public license
 * or a commercial license.
 * You may select to use one or the other of these licenses.
 * To obtain a commercial license please contact Stefan Isele at www.stefan-isele.de
 * Any modifications to this file must keep this entire header intact.
 * Redistribution, except as permitted by the selected license, is prohibited.
 * #L%
 */
package com.prefabware.jmodel;

import java.util.List;

import com.prefabware.commons.CollectionUtil;
import com.prefabware.commons.QualifiedName;
import com.prefabware.commons.beans.MethodReadAccessor;
import com.prefabware.commons.collection.Filter;

public class JEnumeration extends JClass {
	private Filter<? super JField> filter = new Filter<JField>() {

		@Override
		public boolean includes(JField candidate) {
			return candidate instanceof JEnumerationValue;
		}
	};

	public JEnumeration(QualifiedName qn, JTypeDeclaration declaration) {
		super(qn, declaration);
	}

	public List<JEnumerationValue> values() {
		return CollectionUtil.filter(this.getFields(), filter);
	}

	public JEnumerationValue getValue(String name) {
		return CollectionUtil.asPropertyMap(this.values(),new MethodReadAccessor<String>(JMember.METHOD_GET_NAME)).get(name);
	}

	public JEnumeration add(String... names) {
		for (String name : names) {
			add(name);
		}
		return this;
	}

	public JEnumerationValue add(String name) {
		// TODO test if this results is the same as by reflection
		JEnumerationValue ec = JTypeUtil.newJEnumerationConstant(name, this, JModifiers.FINAL_STATIC, JVisibility.PUBLIC);
		this.add(ec);
		return ec;
	}
	@Override
	public String getJavaKeyword() {
		return "enum";
	}
}
